<?php

$qfile = "/tmp/JobsQueue";

if (pcntl_fork() == 0)
{
    // kid
    $file = fopen($qfile,"w");
    sleep(1);
    exit(0);  
}
else
{
    usleep(15); // make sure that the child executes first
    $file = fopen($qfile,"r");
}

echo "opened the queue \n";


while (true)
{
     printf("我是 %d FIFO读取进程.\n", posix_getpid());
     $reders = [$file];
     $writers = [];
     $except = [];
     if (stream_select($reders,$writers,$except,0,15) < 1)
     {
        echo "waiting next..\n";
        sleep(1);
        continue;
     } else {
         // read data from the fifo
         $data = fread($file,1024);
         echo $data;
     }         
     sleep(1);
}
